package com.xymc.webmgr.service.impl;


import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xymc.webmgr.entity.TUser;
import com.xymc.webmgr.dao.db1.TUserDao;
import com.xymc.webmgr.service.TUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;

/**
 * <p>用户表<p>
 * @version : 1.0
 * @Author : li_hao
 * @Description : 服务实现类
 * @Date : 2022-01-28
 **/
@Transactional
@Service
public class TUserServiceImpl implements TUserService {
    public static final Log log = LogFactory.getLog(TUserService.class);

    @Resource
    private TUserDao tUserDao;


    @Value("${retCode0}")
    private String retCode0;
    @Value("${retMsg0}")
    private String retMsg0;
    @Value("${retCode1}")
    private String retCode1;
    @Value("${retMsg1}")
    private String retMsg1;


    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject create(JSONObject reqJson) {
        JSONObject retJson = new JSONObject();
        String retCode = retCode1;
        String retMsg = retMsg1;
        JSONObject bizDataJson = new JSONObject();
        try {
            TUser tUser = JSON.parseObject(reqJson.toJSONString(), TUser.class);
            int count = tUserDao.insert(tUser);
            if (count > 0) {
                retCode = retCode0;
                retMsg = retMsg0;
            }
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject update(JSONObject reqJson) {
        JSONObject retJson = new JSONObject();
        String retCode = retCode1;
        String retMsg = retMsg1;
        JSONObject bizDataJson = new JSONObject();
        try {
            TUser tUser = JSON.parseObject(reqJson.toJSONString(), TUser.class);
            int count = tUserDao.update(tUser);
            if (count > 0) {
                retCode = retCode0;
                retMsg = retMsg0;
            }
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject delete(JSONObject reqJson) {
        JSONObject retJson = new JSONObject();
        String retCode = retCode1;
        String retMsg = retMsg1;
        JSONObject bizDataJson = new JSONObject();
        try {
            TUser tUser = JSON.parseObject(reqJson.toJSONString(), TUser.class);
            int count = tUserDao.delete(tUser);
            if (count > 0) {
                retCode = retCode0;
                retMsg = retMsg0;
            }
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }

    /**
     * 查询详情
     */
    @Override
    public JSONObject getDetail(String id) {
        JSONObject retJson = new JSONObject();
        String retCode = retCode1;
        String retMsg = retMsg1;
        JSONObject bizDataJson = new JSONObject();
        try {
            TUser tUser = new TUser();
            tUser.setId(id);
            tUser = tUserDao.queryDetail(tUser);
            if(tUser != null){
                bizDataJson = JSONObject.parseObject(JSON.toJSONString(tUser));
            }
            retCode = retCode0;
            retMsg = retMsg0;
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }


    /**
     * 模糊查询分页列表
     */
    @Override
    public JSONObject getPageList(JSONObject reqJson) {
        JSONObject retJson = new JSONObject();
        String retCode = "1";
        String retMsg = "操作失败！";
        JSONObject bizDataJson = new JSONObject();
        try {
            int pageNo = reqJson.getInteger("pageNo") == null ? 1 : reqJson.getInteger("pageNo");
            int pageSize = reqJson.getInteger("pageSize") == null ? 10 : reqJson.getInteger("pageSize");
            HashMap<String, Object> reqMap = JSON.parseObject(reqJson.toJSONString(), HashMap.class);

            //查询用户分页列表
            Page<TUser> userListPage = PageHelper.startPage(pageNo, pageSize);
            List<TUser> userList = tUserDao.queryList(reqMap);
            long userTotal = userListPage.getTotal();

            bizDataJson.put("userList", userList);
            bizDataJson.put("userTotal", userTotal);

            retCode = "0";
            retMsg = "操作成功！";
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }

    /**
     * 查询列表（不分页）
     */
    @Override
    public JSONObject getList(JSONObject reqJson) {
        JSONObject retJson = new JSONObject();
        String retCode = "1";
        String retMsg = "操作失败！";
        JSONObject bizDataJson = new JSONObject();
        try {
            HashMap<String, Object> reqMap = JSON.parseObject(reqJson.toJSONString(), HashMap.class);
            List<TUser> userList = tUserDao.queryList(reqMap);
            bizDataJson.put("userList", userList);

            retCode = "0";
            retMsg = "操作成功！";
        } catch (Exception e) {
            log.error(e);
        }
        retJson.put("retCode", retCode);
        retJson.put("retMsg", retMsg);
        retJson.put("bizData", bizDataJson);
        return retJson;
    }

}
